package 力扣._543_二叉树的直径;

import 力扣.公用数据结构.TreeNode;

public class Solution2 {

    void dfs(TreeNode root) {
        if (root == null) {

            return;
        }
        dfs(root.left);
        dfs(root.right);

    }

    //当前子树最大深度
    int maxdeepth(TreeNode root) {
        if (root == null)
            return 0;
        return Math.max(maxdeepth(root.left), maxdeepth(root.right)) + 1;

    }

    //对每个节点使用maxdeepth找到最大
    int max = 0;

    void max(TreeNode root) {
        if (root == null)
            return;
        max(root.left);
        max(root.right);
        //从下向上
        int now_l = maxdeepth(root.left);
        int now_r = maxdeepth(root.right);
        int now = now_l + now_r;
        max = Math.max(max, now);
    }

    public int diameterOfBinaryTree(TreeNode root) {
        if (root == null)
            return 0;
        max(root);
        return max;
    }
}
